<?php
/**
 * @package View
 * @subpackage Widgets
 * @category System
 * @author Dan Krasilnikov <dkrasilnikov@gmail.com>
 * @copyright Copyright (c) 2009, Dan Krasilnikov
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 * @version 0.0.1 alpha  
*/

/**
 * @ignore
 */
require_once 'widgets/areas/TAreasWidget.inc';
require_once 'models/linkers.inc';
require_once 'models/lists.inc';
require_once 'pages/TMSItemPage.inc';

/**
 * @package View
 * @subpackage Widgets
 * @category System
 * command bar
 */
class TAssociationWidget extends TAreasWidget {
	private $_search_condition_ = null;
	
	protected $_array_search_properties_ = array();
	
/**
 * @var IListProvider
 */	
	protected $_ioc_suggestion_model_;
	
	protected function checkSource($src){
		return $src instanceof IAssociator;
	}	
	
	public function http_Find($search){
		if ($search){
			$this->_search_condition_ = $search;
			$this->refreshArea(new TWidgetArea($this, $this, 'Suggestions'));
		}
	}
	
	public function http_Link($link = null){
		if (is_array($link)){
			if (!($this->response instanceof TMSItemPage))
				throw new TCoreException(TCoreException::ERR_BAD_TYPE);
			$this->Source->Associate($this->response->ModelItem, array_keys($link));
			$this->refreshArea(new TWidgetArea($this, $this, 'Assignments'));
		}
	}
	
	public function http_Unlink($link = null){
		if (is_array($link)){
			if (!($this->response instanceof TMSItemPage))
				throw new TCoreException(TCoreException::ERR_BAD_TYPE);
			$this->Source->Deassociate($this->response->ModelItem, array_keys($link));
			$this->refreshArea(new TWidgetArea($this, $this, 'Assignments'));
		}
	}
	
/**
 * gest presenter actions
 * @return array of TPresenterAction
 */
	public function Suggestions(){
		if ($this->_search_condition_ && !empty($this->_array_search_properties_)){
			if (!($this->response instanceof TMSItemPage))
				throw new TCoreException(TCoreException::ERR_BAD_TYPE);
			$conditions = array();
			foreach ($this->SearchProperties as $p)
				$conditions[] = new TFilterOption($p, TConditionType::C_LIKE, $this->_search_condition_);
			if (count($conditions) > 1)
				$conditions = array(new TOrmCondition(TConditionType::C_OR,$conditions));
			return $this->SuggestionModel->GetList(new TListOptions($conditions));
		}
		return new TArrayIterator(array());	
	}
	
/**
 * gest presenter actions
 * @return array of TPresenterAction
 */
	public function Associations(){
		if (!($this->response instanceof TMSItemPage))
			throw new TCoreException(TCoreException::ERR_BAD_TYPE);
		$this->response->ReloadItem();
		return $this->Source->Associations($this->response->ModelItem);
	}
	
/*	protected function areaCssClass(TWidgetArea $area){
		$context = $area->;
		if (is_object($context))
			return get_class($context);
		return null;
	}*/
	
	public function Css(){
		?>
			div.page-assoc {
				border-style:inset;
				border-width:1px;
				padding:5px;
			}
			
			div.page-assoc form {
				border-style:none;
				margin:0px;
				padding:0px;
				margin-bottom:10px;
			}
			
			div.page-assoc div.search-results, div.page-assoc div.assignments {
				height:100px;
				overflow:auto;
				border-style:inset;
				border-width:1px;
			}
			
			div.page-assoc form table {
				font-size:9pt;
			}
			
			div.page-assoc a {
				text-decoration:none;
				color:black;
			}
			
			div.page-assoc a:hover {
				text-decoration:underline;
			}
			
			div.page-assoc div.btn-holder {
				text-align:right;
			}
			
			div.page-assoc div.btn-holder input[type=submit]{
				font-size:12pt;
				padding:4px;
			}
		<?php
	}
	
	protected function areaCssClass(TWidgetArea $area){
		if ($area->Skin() == 'Assignments')
			return 'assignments';
		return parent::areaCssClass($area);
	}
	
	protected function renderSuggestions(TWidgetArea $area){
		$list = $this->Suggestions();
		if ($list->ItemCount() > 0) { ?>
		<div class="search-results">
		<table>
<?php		foreach ($list as $p){ ?>
				<tr class="page"><td><input type="checkbox" name="link[<?php echo $p->ItemId(); ?>]" /></td><td><a href="<?php echo $this->response->ItemEditUrl($p); ?>"><?php echo $p; ?></a></td></tr>
<?php		} ?>
		</table>
		</div>
		<div class="btn-holder"><input type="submit" value="<?php __i('Add'); ?>" /></div>
<?php
		}
	}
	
	protected function renderAssignments(TWidgetArea $area){
		$list = $this->Associations();
?>
<table>
<?php		
		foreach ($list as $p){
?>
<tr class="page"><td><input type="checkbox" name="link[<?php echo $p->ItemId(); ?>]" /></td><td><a href="<?php echo $this->response->ItemEditUrl($p); ?>"><?php echo $p; ?></a></td></tr>
<?php			
		}
?>
</table>
<?php		
	}

/**
 * @see TWidget::DefaultOutput()
 */	
	protected function defaultOutput(IThemeEngine $engine){
?>
<div id="<?php echo $this->Name() ?>" class="page-assoc">
<form id="<?php echo $this->Name() ?>_search" method="post" action="<?php $this->response->Url(); ?>">
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="Find" />
	<input type="text" name="search" value="" />
	<input type="submit" value="<?php __i('Find'); ?>" />
</form>
<form id="assoc" method="post" action="<?php $this->response->Url(); ?>">
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="Link" />
<?php 
$this->renderArea($this->GetArea($this,"Suggestions"));
?>
</form>
<form id="deassoc" method="post" action="<?php $this->response->Url(); ?>">
	<input type="hidden" name="controller" value="<?php echo $this->Name(); ?>" />
	<input type="hidden" name="method" value="Unlink" />
	<?php
	$this->renderArea($this->GetArea($this,"Assignments"));
	?>
	<div class="btn-holder"><input type="submit" value="<?php __i('Remove'); ?>" /></div>
</form>
</div>
<script language="javascript">
	$("#<?php echo $this->Name(); ?>").areas_ajax_forms();
</script>
<?php		
	}
	
/**
 * @see TJQueryWidget::linkCustomResources()
 */
	protected function linkCustomResources(){
		parent::linkCustomResources();
		$this->loadForms();
	}	
}
